package com.kevin.spring.reference;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * 为传入的bean对象，生成新的动态代理对象，放入工厂中
 */
@Slf4j
public class SpringRpcReferenceBean implements FactoryBean<Object> {
    private Object object;

    private Class<?> interfaceClass;

    private String serverIp;

    private int serverPort;

    @Override
    public Object getObject() throws Exception {
        return object;
    }

    @Override
    public Class<?> getObjectType() {
        return this.interfaceClass;
    }

    public void init(){
        this.object = Proxy.newProxyInstance(interfaceClass.getClassLoader(),
                new Class<?>[]{interfaceClass}, new RpcInvocationHandler(serverIp,serverPort));
    }


    public void setObject(Object object) {
        this.object = object;
    }

    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}
